using System;
using System.Drawing;
using System.Windows.Forms;

namespace ctsu.Admin.Forms.CoreLab
{
	public partial class SampleChooser4Dlg : Form
	{
		public delegate void SelectSampleButtonClickEventHandler(object sender, SelectSamplesEventArgs e);
		public event SelectSampleButtonClickEventHandler SelectSampleClick;

		public SampleChooser4Dlg()
		{
			InitializeComponent();
			this.Num_ID.KeyPress += new KeyPressEventHandler(Forms.Utils.txtNumWholePos_KeyPress);
		}

		private void Num_ID_TextChanged(object sender, System.EventArgs e)
		{
			if (Num_ID.Text.Length > 0)
			{
				if ((Convert.ToInt32(Num_ID.Text)) > 0)
					Retrieve.Enabled = true;
				else
					Retrieve.Enabled = false;
			}
			else
				Retrieve.Enabled = false;
		}

		private void Retrieve_Click(object sender, System.EventArgs e)
		{

			SelectSamplesEventArgs ev = new SelectSamplesEventArgs();
			if (Num_ID.Visible)
				ev.ID = Convert.ToInt32(Num_ID.Text);
			else
				ev.Location = GetFormatatedLocation();

			if (rdoBatch.Checked)
				ev.SelectType = 0;
			else if (rdoManual.Checked)
				ev.SelectType = 1;
			else
				ev.SelectType = 2;

			if (SelectSampleClick != null)
				this.SelectSampleClick(this, ev);
			if (Num_ID.Visible)
				this.Num_ID.Focus();
			else
				gbxLocation.Focus();
		}

		private string GetFormatatedLocation()
		{
			string location = "Fz:" + Freezer.SelectedValue.ToString() + "," +
				"Sf:" + Shelf.Value.ToString() + "," +
				"Rk:" + Rack.Value.ToString() + "," +
				"Bx:" + Box.Value.ToString();
			return location;
		}

		private void rdo_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.rdoBatch.Checked)
			{
				gbxLocation.Visible = false;
				lblNum_ID.Visible = true;
				Num_ID.Visible = true;
				lblNum_ID.Text = "Enter Batch Number:";
				this.Num_ID.Focus();
			}
			else if (rdoManual.Checked)
			{
				gbxLocation.Visible = false;
				lblNum_ID.Visible = true;
				Num_ID.Visible = true;
				lblNum_ID.Text = "Enter Label ID:";
				this.Num_ID.Focus();
			}
			else
			{
				Retrieve.Enabled = true;
				lblNum_ID.Visible = false;
				Num_ID.Visible = false;
				gbxLocation.Location = new Point(8, 96);
				gbxLocation.Visible = true;
				gbxLocation.Focus();
			}

			if (Num_ID.Visible)
			{
				if (Num_ID.Text.Length == 0)
					Retrieve.Enabled = false;
			}

		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void SampleChooser4Dlg_Load(object sender, EventArgs e)
		{
			Forms.Utils.LoadCbo(Freezer, "Freezer_L", true);
		}
	}
}